package com.yuntsg.ruiijn.controller.search;

import com.yuntsg.ruiijn.databse_search.service.EsWebService;
import com.yuntsg.ruiijn.paperana.entity.BackMatchEntity;
import com.yuntsg.ruiijn.paperana.utils.esutils.EsCreate;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import common.util.CheckUtil;
import common.util.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2023/10/11 20:48
 */

@RestController
@RequestMapping("web/pub/")
@SuppressWarnings("all")
@Slf4j
public class WebController {

    @Resource
    EsWebService esWebService;

    @Resource
    EsCreate esCreate;


    @PostMapping("/getes")
    public ResultBean<Object> getEsWebInfo(Integer type, Integer source, Integer page, String content, Integer fid, Integer uid) {
        // 获取es 结果用
        List<Object> hits = esWebService.esBackData(type, source, page, content);
        if (hits != null && hits.size() > 0) {
            return new ResultBean<>(hits);
        } else {
            CheckUtil.check(false, "失败");
            return new ResultBean<>("");
        }
    }

    @SneakyThrows
    @GetMapping("escreate")
    public String escreate(String s) {
        if (s != null && s.equals("asdf123456852")) {
            esCreate.start();
            return "succ";
        } else {
            return "aaa哈哈哈";
        }
    }

    @PostMapping("/getesprenex")
    public ResultBean<Object> getEsWebInfoPreNext(String dataType, Integer sentence, String subProject, String file_id, String table_no) {
        // 获取es 上下句
        String res = esWebService.esBackDataPreNext(dataType, sentence, subProject, file_id, table_no);
        if (StringUtil.isNotNull(res)) {

            return new ResultBean<>(res);
        } else {
            CheckUtil.check(false, "失败");
            return new ResultBean<>("");
        }
    }

    @PostMapping("/getpminfo")
    public ResultBean<Object> getEsWebInfo(Integer tableId, Integer fileId, String matchStr, String isManual) {
        BackMatchEntity tableInfo = esWebService.findTableInfo(tableId, fileId, matchStr, isManual);
        if (StringUtil.isNotNull(tableInfo)) {
            return new ResultBean<>(tableInfo);
        } else {
            CheckUtil.check(false, "查询失败");
            return new ResultBean<>(tableInfo);
        }

    }

}
